/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pas.spaceandtime.view.tabbedpane;

import java.beans.PropertyChangeEvent;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import za.co.pas.business.controller.CompanyDetailController;
import za.co.pas.business.message.MessageObj;
import za.co.pas.business.abstracts.AbstractView;
import za.co.pas.business.constant.Constant;
import za.co.pas.business.model.ext.CompanyDetailModel;
import za.co.pas.business.mvc.BusinessMVC;
import za.co.pas.business.view.panel.CompanyListPanel;
import za.co.pas.business.view.panel.CompanyDetailPanel;
import za.co.pas.spaceandtime.controller.BuildingDetailController;
import za.co.pas.spaceandtime.message.MsgConstant;
import za.co.pas.spaceandtime.view.panel.BuildingDetailPanel;
import za.co.pas.spaceandtime.view.panel.BuildingListPanel;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.SwingUtilities;
import za.co.pas.spaceandtime.view.panel.ConfigAndTypePanel;
/**
 *
 * @author alabuschagne
 */
public class TabbedPane extends JTabbedPane implements AbstractView
{

    public static final int COMPANY_LIST = 0;
    public static final int COMPANY_DETAILS = 1;
    public static final int BUILDING_LIST = 2;
    public static final int BUILDING_DETAILS = 3;
    public static final int CONFIG = 4;
    public static final String[] TAB_NAMES = {  "Company List",
                                                "Company Details",
                                                "Building List",
                                                "Building Details",
                                                "Config"};
    private CompanyDetailController companyDetailController;
    private BuildingDetailController buildingDetailController;

    private CompanyDetailPanel companyPanel;
    private CompanyListPanel companyListPanel;
    private BuildingListPanel buildingListPanel;
    private BuildingDetailPanel buildingDetailPanel;
    private ConfigAndTypePanel configAndTypePanel;

    private Timer timer = null;
    private TimerTask timerTask = new TimerTask()
    {

        @Override
        public void run()
        {
            SwingUtilities.invokeLater(new Runnable()
            {

                @Override
                public void run()
                {
                    checkTabs();
                }


            });
        }
    };
    private TabbedPane()
    {
    }

    public TabbedPane(CompanyDetailController companyDetailController, BuildingDetailController buildingDetailController)
    {
        super();
        this.companyDetailController = companyDetailController;
        this.buildingDetailController = buildingDetailController;
        this.addChangeListener(new ChangeListener()
        {

            @Override
            public void stateChanged(ChangeEvent evt)
            {
                companyTabbedPaneStateChanged(evt);
            }
        });
    }

    public void init()
    {
        companyListPanel = new CompanyListPanel(companyDetailController);
        companyPanel = new CompanyDetailPanel(companyDetailController);
        buildingListPanel = new BuildingListPanel();
        buildingDetailPanel = new BuildingDetailPanel(buildingDetailController);
        configAndTypePanel = new ConfigAndTypePanel();
        companyDetailController.addView(companyListPanel);
        companyDetailController.addView(companyPanel);
        companyDetailController.addView(buildingListPanel);
        companyDetailController.addView(this);
        buildingDetailController.addView(buildingListPanel);
        buildingDetailController.addView(buildingDetailPanel);
        buildingDetailController.addView(this);
        this.addTab(TAB_NAMES[COMPANY_LIST], companyListPanel);
        this.addTab(TAB_NAMES[COMPANY_DETAILS], companyPanel);
        this.addTab(TAB_NAMES[BUILDING_LIST], buildingListPanel);
        this.addTab(TAB_NAMES[BUILDING_DETAILS], buildingDetailPanel);
        this.addTab(TAB_NAMES[CONFIG], configAndTypePanel);
        this.setEnabledAt(COMPANY_DETAILS, false);
        this.setEnabledAt(BUILDING_LIST, false);
        this.setEnabledAt(BUILDING_DETAILS, false);
        timer = new Timer();
        timer.scheduleAtFixedRate(timerTask, 0, 1000);
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName().compareTo(MessageObj.COMPANY_LIST_CHANGED) == 0)
        {
            CompanyDetailModel cdm = (CompanyDetailModel) Constant.mvc.getModel(BusinessMVC.COMPANY_DETAIL_MODEL);
            if (cdm.getCurrentCompany() == null)
            {
                this.setEnabledAt(COMPANY_DETAILS, false);
                this.setEnabledAt(BUILDING_LIST, false);
                this.setEnabledAt(BUILDING_DETAILS, false);
            }
        }
        if((evt.getPropertyName().compareTo(MessageObj.COMPANY_DETAILS_CHANGED) == 0)
                || (evt.getPropertyName().compareTo(MessageObj.REQUEST_COMPANY_DETAILS_CHANGE) == 0))
        {
            //go to the company detail tab
            int k = this.getSelectedIndex();
            if (k != COMPANY_DETAILS)
            {
                this.setSelectedIndex(COMPANY_DETAILS);
            }
            this.setEnabledAt(COMPANY_DETAILS, true);
            this.setEnabledAt(BUILDING_LIST, true);
            this.setEnabledAt(BUILDING_DETAILS, false);
        }
        if(evt.getPropertyName().compareTo(MsgConstant.BUILDING_DETAIL_CHANGED) == 0)
        {
            int k = this.getSelectedIndex();
            if (k != BUILDING_DETAILS)
            {
                this.setSelectedIndex(BUILDING_DETAILS);
            }
            this.setEnabledAt(COMPANY_DETAILS, true);
            this.setEnabledAt(BUILDING_LIST, true);
            this.setEnabledAt(BUILDING_DETAILS, true);
        }
    }

    private void companyTabbedPaneStateChanged(ChangeEvent evt)
    {
    }

    @Override
    public boolean isDirty()
    {
        return false;
    }

    private void checkTabs()
    {
        StringBuilder str = new StringBuilder(TAB_NAMES[COMPANY_LIST]);
        if(companyListPanel.isDirty())
        {
            str.append(" [*] ");
        }
        this.setTitleAt(COMPANY_LIST, str.toString());

        str = new StringBuilder(TAB_NAMES[COMPANY_DETAILS]);
        if(companyPanel.getExtraTitle() != null)
        {
            str.append(" - ");
            str.append(companyPanel.getExtraTitle());
        }
        if(companyPanel.isDirty())
        {
            str.append(" [*] ");
        }
        this.setTitleAt(COMPANY_DETAILS, str.toString());

        str = new StringBuilder(TAB_NAMES[BUILDING_LIST]);
        if(buildingListPanel.isDirty())
        {
            str.append(" [*] ");
        }
        this.setTitleAt(BUILDING_LIST, str.toString());

        str = new StringBuilder(TAB_NAMES[BUILDING_DETAILS]);
        if(buildingDetailPanel.isDirty())
        {
            str.append(" [*] ");
        }
        this.setTitleAt(BUILDING_DETAILS, str.toString());
    }

        @Override
    public String getExtraTitle()
    {
        return null;
    }
}
